package cn.com.dhcc.turbo.service.monitor.compute;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.turbo.entity.monitor.Quota;
import cn.com.dhcc.turbo.entity.monitor.QuotaComputeRule;
import cn.com.dhcc.turbo.entity.monitor.QuotaConf;
import cn.com.dhcc.turbo.entity.monitor.QuotaValueOnTime;
import cn.com.dhcc.turbo.entity.monitor.QuotaValueOnTime.QuotaHadlerStatus;
import cn.com.dhcc.turbo.service.monitor.MonitorService;
import cn.com.dhcc.turbo.service.monitor.MonitorQuotaStatic.ALERT_PIPELINE;


/**
 * 保留当天时间段内的数据的指标处理
 * @author qiao
 * @2014-6-4 @下午2:31:16
 */
@Service
public class CurrentDayRowsCompute implements MonitorComputeInterface{

	@SuppressWarnings("deprecation")
	Logger logger = Log.getExistLogger(LOGTYPE.TURBO);
	@Autowired
	MonitorService monitorService;
	@Autowired
	CommonCompute commonMonitorHandler;
	@Override
	public void merge(List<QuotaValueOnTime> onTimes,Quota quota,QuotaComputeRule computeRule,QuotaConf conf) throws ServiceException {
		commonMonitorHandler.merge(onTimes, quota, computeRule, conf);
	}

	@Override
	public void computeAlert(List<QuotaValueOnTime> onTimes,Quota quota,QuotaComputeRule computeRule,QuotaConf conf) throws ServiceException, NiceException {
		commonMonitorHandler.computeAlert(onTimes, quota, computeRule, conf);
	}

	@Override
	public void moveToHistory(List<QuotaValueOnTime> onTimes,Quota quota,QuotaComputeRule computeRule,QuotaConf conf) throws ServiceException {
		moveBeforeTimeRowsToHistory(onTimes,computeRule,conf);
	}
	/**
	 * 保留当天的数据，一般针对不需要告警的指标
	 * @param onTimes
	 * @throws ServiceException 
	 */
	DateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
	public void moveBeforeTimeRowsToHistory(List<QuotaValueOnTime> onTimes,QuotaComputeRule computeRule,QuotaConf conf) throws ServiceException{
		String today = dayFormat.format(new Date());
		List<QuotaValueOnTime> onTimesToMove = new ArrayList<QuotaValueOnTime>();
		Map<String, List<QuotaValueOnTime>> quotaValueNotRemoveGroupByOwnerId = new HashMap<String, List<QuotaValueOnTime>>();
		//开始按告警归属ownerId分组
		for (Iterator<QuotaValueOnTime> iterator = onTimes.iterator(); iterator
				.hasNext();) {
			QuotaValueOnTime quotaValueOnTime = iterator.next();
			String ownerId = quotaValueOnTime.getOwnerId();
			if(quotaValueNotRemoveGroupByOwnerId.get(ownerId)==null){
				quotaValueNotRemoveGroupByOwnerId.put(ownerId, new ArrayList<QuotaValueOnTime>());
			}
			if(quotaValueNotRemoveGroupByOwnerId.get(ownerId).size()==1){
				if(!dayFormat.format(quotaValueOnTime.getMtime()).equals(today)){
					onTimesToMove.add(quotaValueOnTime);
					continue;
				}
			}
			quotaValueNotRemoveGroupByOwnerId.get(ownerId).add(quotaValueOnTime);//必须保留一条（最新的）
		}
		Date handlerTime = new Date();
		for (Iterator<QuotaValueOnTime> iterator = onTimesToMove.iterator(); iterator.hasNext();) {
			QuotaValueOnTime quotaValueOnTime = iterator.next();
			if(!StringUtils.hasText(quotaValueOnTime.getHasAlertMethod())){
				quotaValueOnTime.setHandlerTime(handlerTime);
				quotaValueOnTime.setHasAlertMethod(ALERT_PIPELINE.AUTO_CLOSE.getCode());
				quotaValueOnTime.setStatus(QuotaHadlerStatus.HAS_HANDLER.getCode());
			}
			monitorService.moveOnTimeMonitorToHistory(quotaValueOnTime);
		}
	}
	
}
